﻿using DotNetCore.CAP;
using Microsoft.AspNetCore.Mvc;
using MySqlConnector;
using System;
using System.Data;
using System.Threading.Tasks;

namespace Sample.MQTT.Mysql.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class ValuesController : ControllerBase
    {
        private readonly ICapPublisher _capBus;

        public ValuesController(ICapPublisher capPublisher)
        {
            _capBus = capPublisher;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        [Route("~/without/transaction")]
        [HttpGet]
        public async Task<IActionResult> WithoutTransaction()
        {
            await _capBus.PublishAsync("sample.rabbitmq.mysql", DateTime.Now);

            return Ok();
        }
        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        [Route("~/without/transaction1")]
        [HttpGet]
        public async Task<IActionResult> WithoutTransaction1()
        {
            await _capBus.PublishAsync("sample.rabbitmq.mysql1", DateTime.Now);

            return Ok();
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="val"></param>
        /// <returns></returns>
        [Route("~/test")]
        [HttpGet]
        public async Task<IActionResult> Test(int val)
        {
            await _capBus.PublishAsync("cap/sample.test", val, "cap/sample.test.result");

            return Ok();
        }
        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        [Route("~/null")]
        [HttpGet]
        public async Task<IActionResult> TestNull()
        {
            DateTime? send = null;
            await _capBus.PublishAsync("cap/sample.test.null", send);

            return Ok();
        }
        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        [Route("~/adonet/transaction")]
        [HttpGet]
        public IActionResult AdonetWithTransaction()
        {
            //using (var connection = new MySqlConnection(AppDbContext.ConnectionString))
            //{
            //    using (var transaction = connection.BeginTransaction(_capBus, true))
            //{
            //your business code
            //connection.Execute("insert into test(name) values('test')", transaction: (IDbTransaction)transaction.DbTransaction);

            //for (int i = 0; i < 5; i++)
            //{
            _capBus.Publish("sample.rabbitmq.mysql", DateTime.Now);
            //}
            //    }
            //}

            return Ok();
        }
        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        [Route("~/ef/transaction")]
        [HttpGet]
        public IActionResult EntityFrameworkWithTransaction(/*[FromServices] AppDbContext dbContext*/)
        {
            //using (var trans = dbContext.Database.BeginTransaction(_capBus, autoCommit: false))
            //{
            //    dbContext.Persons.Add(new Person() { Name = "ef.transaction" });

            for (int i = 0; i < 1; i++)
            {
                _capBus.Publish("sample.rabbitmq.mysql", DateTime.Now);
            }

            //    dbContext.SaveChanges();

            //    trans.Commit();
            //}
            return Ok();
        }
        [NonAction]
        [CapSubscribe("cap/sample.test")]
        public int Calc(int p)
        {
            Console.WriteLine($@"{DateTime.Now} Calc invoked, Info: {p}");
            return p + 5;
        }
        [NonAction]
        [CapSubscribe("cap/sample.test.null")]
        public void TestNull(DateTime? send)
        {
            Console.WriteLine($@"{DateTime.Now} TestNull invoked, Info: {send}");
        }
        [NonAction]
        [CapSubscribe("cap/sample.test.result")]
        public void CalcResult(int p)
        {
            Console.WriteLine($@"{DateTime.Now} CalcResult invoked, val: {p}");
        }
        [NonAction]
        [CapSubscribe("sample.rabbitmq.mysql")]
        public void Subscriber(DateTime p)
        {
            Console.WriteLine($@"{DateTime.Now} Subscriber invoked, Info: {p}");
        }

        [NonAction]
        [CapSubscribe("sample.rabbitmq.mysql", Group = "group.test2")]
        public void Subscriber2(DateTime p, [FromCap] CapHeader header)
        {
            Console.WriteLine($@"{DateTime.Now} Subscriber2 invoked, Info: {p} head:{header}");
        }
    }
}
